﻿using System;
using System.Net;
using System.Threading.Tasks;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using Sharp.DataTransferObject;
using Sharp.Infrastructure;
using Sharp.Infrastructure.WebApiClient;
using Sharp.Portal.ExtNet;
using Sharp.Portal.Mvc;
using Sharp.ServiceContracts;
using Sharp.WebBooter.Areas.Platform.Models;
using PartialViewResult = Ext.Net.MVC.PartialViewResult;

namespace Sharp.WebBooter.Areas.Platform.Controllers
{
    [Authorize]
    [AutomaticTemplate]
    public class GroupController : Controller
    {
        // GET: Manage/Group
        public ActionResult Index(string containerId)
        {
            return new PartialViewResult
            {
                Model = new GroupIndexModel(),
                ContainerId = containerId,
                RenderMode = RenderMode.AddTo
            };
        }
        /// <summary>
        ///     分页数据
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetPaged(ExtRequestParameters parameters)
        {
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                var pageInfo = proxy.Channel.GetGroupPaged(parameters.ToPagingFeature());

                return this.Store(pageInfo.Data, pageInfo.TotalRecords);
            }
        }
        // GET: Manage/Group/Details/5
        public async Task<ActionResult> Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            GroupDTO dto;
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                dto = proxy.Channel.GetGroupInfo(id.Value);
            }
            if (dto == null)
            {
                return HttpNotFound();
            }

            var viewModel = dto.To<GroupModel>();
            viewModel.OrgTree = await OrgController.OrgTree();

            return new PartialViewResult { Model = viewModel };
        }

        // GET: Manage/Group/Create
        public async Task<ActionResult> Create()
        {
            return new PartialViewResult
            {
                Model = new GroupModel()
                {
                    OrgTree = await OrgController.OrgTree()
                }
            };
        }


        // GET: Manage/Group/Edit/5
        public async Task<ActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            GroupDTO dto;
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                dto = proxy.Channel.GetGroupInfo(id.Value);
            }
            if (dto == null)
            {
                return HttpNotFound();
            }

            var viewModel = dto.To<GroupModel>();
            viewModel.OrgTree = await OrgController.OrgTree();


            return new PartialViewResult { Model = viewModel };
        }


        [System.Web.Mvc.HttpPost]
        public ActionResult Save(GroupDTO group)
        {
            Result res;
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                res = proxy.Channel.SaveGroup(group);
            }
            return this.JsonNet(res);
        }

        [System.Web.Mvc.HttpPost]
        public ActionResult Delete(int[] ids)
        {
            Result res;
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                res = proxy.Channel.DeleteGroups(ids);
            }

            return this.JsonNet(res); 
        }


    }
}
